package com.five.calendar.ui.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.five.calendar.R;
import com.five.calendar.database.bean.ScheduleBean;
import com.five.calendar.ui.activity.ScheduleAddActivity;
import com.five.calendar.utils.TimeUtils;

import java.util.List;

/**
 * Created by xin on 2017/9/1.
 */

public class ScheduleListAdapter extends BaseAdapter {
    private List<ScheduleBean> mList;
    private Context mContext;
    private ViewHolder mHolder;
    private String mDay = "";
    private int[] mItemBgResources = new int[]{R.drawable.calendar_round_corner_bg1, R.drawable.calendar_round_corner_bg2, R.drawable.calendar_round_corner_bg3, R.drawable.calendar_round_corner_bg4};
    public ScheduleListAdapter(List<ScheduleBean> list, Context context){
        mList = list;
        mContext = context;
    }
    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(convertView == null){
            convertView = LayoutInflater.from(mContext).inflate(R.layout.schedule_item_with_day, parent, false);
            mHolder = new ViewHolder();
            mHolder.dayView = (TextView) convertView.findViewById(R.id.item_day);
            mHolder.titleView = (TextView) convertView.findViewById(R.id.item_title);
            mHolder.detailView = (TextView) convertView.findViewById(R.id.item_detail);
            mHolder.timeView = (TextView) convertView.findViewById(R.id.start_time);
            mHolder.itemLayout = (LinearLayout) convertView.findViewById(R.id.item_layout);
            mHolder.endTimeView = (TextView) convertView.findViewById(R.id.end_time);
            convertView.setTag(mHolder);
        } else {
            mHolder = (ViewHolder) convertView.getTag();
        }
        final ScheduleBean scheduleBean = mList.get(position);
        String monthAndDay = TimeUtils.timeFormatMonthDay(scheduleBean.startTime);
        String time = TimeUtils.timeFormatString(scheduleBean.startTime);
        String endTime = TimeUtils.timeFormatString(scheduleBean.endTime);
        if(monthAndDay.equals(mDay)){
            mHolder.dayView.setVisibility(View.GONE);
            mHolder.titleView.setText(scheduleBean.title);
            mHolder.detailView.setText(scheduleBean.note);
            mHolder.timeView.setText(time);
            mHolder.endTimeView.setText(endTime);
            mHolder.itemLayout.setBackgroundResource(mItemBgResources[scheduleBean.importance]);
        }else{
            mHolder.dayView.setText(monthAndDay);
            mHolder.titleView.setText(scheduleBean.title);
            mHolder.detailView.setText(scheduleBean.note);
            mHolder.timeView.setText(time);
            mHolder.endTimeView.setText(endTime);
            mHolder.itemLayout.setBackgroundResource(mItemBgResources[scheduleBean.importance]);
            mDay = monthAndDay;
        }
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ScheduleAddActivity.startScheduleAddActivity(scheduleBean);
            }
        });
        return convertView;
    }

    class ViewHolder{
        public TextView dayView;
        public TextView titleView;
        public TextView detailView;
        public TextView timeView;
        public TextView endTimeView;
        public LinearLayout itemLayout;
    }
}
